import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormBuilder, Validators } from '@angular/forms';
import { ApiService } from '../../../core/api/api.service';
import { MessageService } from '../../../core/service/message.service';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { DatePipe } from '@angular/common';

/**
 * 任务管理-新建任务
 */

@Component({
    templateUrl: './taskAdd.html',
    styleUrls:['./taskAdd.scss']
})
export class TaskAddComponent implements OnInit {
    validForm;
    areaList:Array<any> = [];
    enterpriseList = [];
    pocList = [];
    protocolList = [];
    vulnerabilityList = [];
    ngOnInit() {
        this.validForm = this.fb.group({
            name: ['',[Validators.required]],
            type:['',[Validators.required]],
            type2:['',[Validators.required]],
            startTime:['',[Validators.required]],
            endTime:['',[Validators.required]],
            designateAreaBool:['',[Validators.required]],
            designateArea:['N',[Validators.required]],
            taskCityList:['',[Validators.required]],
            keyLevel:['',[Validators.required]],
            designateEnterprise:["Y",[Validators.required]],
            enterpriseId:['',[Validators.required]],
            vulnerabilityId:['',[Validators.required]],
            pocId:['',[Validators.required]],
            allIp:['',[Validators.required]],
            ipRange:['',[Validators.required]],
            port:['',[Validators.required]],
            protocolId:['',[Validators.required]],
            bandWidth:['',[Validators.required]],
            desc:['',[Validators.required]],
            dateRange:['',[Validators.required]],
        })
        this.apiService.post('获取各个区',{"provinceId":"","cityId":"3206","areaId":""}).then((res:any)=>{
            this.areaList = res.data;
        });
        this.apiService.post('Poc列表(下拉框)',{}).then((res:any)=>{
            this.pocList = res.data.list;
        });
        this.apiService.post('协议列表(下拉框)',{}).then((res:any)=>{
            this.protocolList = res.data.list;
        });
        this.apiService.post('漏洞列表(下拉框)',{}).then((res:any)=>{
            this.vulnerabilityList = res.data.list;
        });
        this.apiService.post('企业列表',{
            pageNo:1,
            pageSize:100,
            total:0,
            keyLevel:'',
            cityId:'3206',
            areaId:'',
            status:'',
            name:'',
        }).then((res:any)=>{
            this.enterpriseList = res.data.list;
        })
    }
    constructor(
        private router: Router,
        private fb: FormBuilder,
        private apiService: ApiService,
        private message: MessageService,
        private nzMessageService: NzMessageService,
        private datePipe: DatePipe,
        private modal: NzModalService
    ) { }

    onChange(value){
        if(value&&value.lenght>1){
            this.validForm.patchValue({startTime:this.datePipe.transform(value[0],'yyyy-MM-dd HH:mm:ss'),endTime:this.datePipe.transform(value[1],'yyyy-MM-dd HH:mm:ss')});
        }
    }
    onChange2(value){
        console.log(value);
        this.areaList.forEach((e:any)=>{e.selected=value;})
        const areaList = this.areaList.filter((e:any)=>{
            return e.selected;
        }).map((e:any)=>{
            return { cityId: '3206', cityName: '南通市', areaId: e.id, areaName: e.name }
        });
        this.validForm.controls.designateArea.setValue(value?"Y":"N");
        this.validForm.controls.taskCityList.setValue(areaList);
    }
    changeIp(v){
        if(v === 'Y'){
            this.validForm.patchValue({
                ipRange:'',
                port:'',
                protocolId:'',
            });
        }
    }
    submit(){
        console.log(this.validForm.value);
        this.apiService.post('创建任务',this.validForm.value).then(res=>{
            this.nzMessageService.success('任务创建成功！');
            this.router.navigateByUrl('/taskMgmt/taskList');
        })
    }
}
